package com.pignest.hotel.ui.room;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;
import org.zkoss.zul.Vbox;

import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.holder.RoomHolder;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.UIUtils;

public class RoomButton extends Span implements EventListener, RoomHolder {

	private static final long serialVersionUID = 3460797661644933202L;
	private static final Log LOG = LogFactory.getLog(RoomButton.class);

	private String roomId;
	private Room room;
	
	private Label nameLabel;
	private Image statusImage;
	private Image liveStatusImage;
	
	public RoomButton() {
		addEventListener("onCreate", this);
	}
	
	@Override
	public void onEvent(Event event) throws Exception {
		LOG.debug(event.getName());
		if ("onCreate".equals(event.getName()))
			onCreate();
	}
	
	private void onCreate() {
		if (StringUtils.isEmpty(roomId))
			return;
		RoomService roomService = Services.getRoomService();
		room = roomService.getRoom(roomId);
		setRoom(room);
	}
	
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}

	public void setRoom(Room room) {
		if (room == null)
			return;
		this.room = room;
		UIUtils.removeAllChildren(this);
		
		Vbox container = new Vbox();
		container.setWidth("100px");
		container.setHeight("100px");
		
//		image = new Image();
//		image.setWidth("48px");
//		image.setHeight("48px");
//		image.setSrc("/images/one.png");
//		container.appendChild(image);
		
		nameLabel = new Label();
		nameLabel.setValue(room.getName());
		nameLabel.setStyle("padding:5px;margin:5px;align:center;");
		nameLabel.setWidth("100%");
		container.appendChild(nameLabel);
		
		Hbox statusContainer = new Hbox();
		statusContainer.setWidth("100%");
		statusImage= new Image();
		statusImage.setWidth("48px");
		statusImage.setHeight("48px");
//		statusImage.setSrc(room.getStatusImage());
		statusContainer.appendChild(statusImage);
		liveStatusImage = new Image();
		liveStatusImage.setWidth("48px");
		liveStatusImage.setHeight("48px");
//		liveStatusImage.setSrc(room.getLiveStatusImage());
		statusContainer.appendChild(liveStatusImage);
		container.appendChild(statusContainer);
		
		container.setAlign("center");
		appendChild(container);
		container.setStyle("border:2px solid #eeeeff;");
		
//		setTooltip("tooltip");
		setPopup("tooltip");
	}
	
	public Room getRoom() {
		return room;
	}
	
	@Override
	public String toString() {
		return "B:" + room;
	}
}